package com.lntea.jdbc.jdk;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

public class StatementDemo {
	public static void main(String[] args) {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
		Connection conn = null;
		Statement statement = null;
		ResultSet resultSet = null;
		
		try {
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jfinalweb", "root", "root");
			statement = conn.createStatement();
			resultSet = statement.executeQuery("select * from users");
			while(resultSet.next()){
				ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
				for(int i=1;i<=resultSetMetaData.getColumnCount();i++){
					String columnLabel = resultSetMetaData.getColumnLabel(i);
					String columnName = resultSetMetaData.getColumnName(i);
					System.out.println("columnLabel:"+columnLabel+",columnName:"+columnName);
					Object column = resultSet.getObject(columnLabel);
					System.out.println(columnLabel+":"+column);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if(resultSet!=null)
				try {
					resultSet.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			if(statement!=null)
				try {
					resultSet.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			if(conn!=null)
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
		}
	}
}
